#!/bin/bash

TARGET_VALUE="${1:-1024}"  # 使用参数或默认值1024

# 遍历所有物理块设备
find /sys/block -mindepth 1 -maxdepth 1 -type l | while read -r device; do
    devname=$(basename "$device")
    
    # 排除虚拟设备
    [[ "$devname" == loop* ]] && continue
    [[ "$devname" == ram* ]] && continue
    [[ "$devname" == dm-* ]] && continue
    
    nr_file="$device/queue/nr_requests"
    
    # 检查可写性
    if [ -f "$nr_file" ] && [ -w "$nr_file" ]; then
        current_val=$(cat "$nr_file")
        echo "设备 $devname: 当前 nr_requests=$current_val, 设置成 $TARGET_VALUE"
        
        # 尝试写入并验证
        if echo "$TARGET_VALUE" > "$nr_file" 2>/dev/null; then
            new_val=$(cat "$nr_file")
            [ "$new_val" -eq "$TARGET_VALUE" ] && status="成功" || status="失败（值未生效）"
        else
            status="失败（权限拒绝）"
        fi
        
        echo "  -> 操作状态: $status"
    fi
done

# 检查是否无设备处理
if ! find /sys/block -mindepth 1 -maxdepth 1 -name 'sd*' -o -name 'nvme*' -o -name 'vd*' | grep -q .; then
    echo "警告：未发现物理磁盘设备（sda/nvme/vd等）" >&2
fi